//
//  LGCultureNameDetailViewController.m
//  ligenyun
//
//  Created by Mac on 2018/4/4.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGFindGenViewController.h"
#import "LGFindGenTableViewCell.h"
#import "LGFindGenCreateNewViewController.h"
#import "LGXunGenNewDetailViewController.h"
@interface LGFindGenViewController ()
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation LGFindGenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"寻根问祖";
    [self configSubView];
    [self configTableHeader];
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf = self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, 49)
                                                         andTitle:@[@"寻根文化",@"寻根信息",@"寻根新闻"]
                                                           handle:^(STSegementButton *sender) {
                                                               
                                                               [weakSelf configTableHeader];
                                                           }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_redColor;
    self.segementButton.butTitleSelectedColor = TM_redColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.frame = CGRectMake(0, self.segementButton.bottom, UIScreenWidth, [TMUtils tabBarTop]+49 - self.segementButton.bottom);
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds];
    bannerImageView.image = [UIImage imageNamed:@"宗祠资料bg"];
  //  self.tableView.backgroundView = bannerImageView;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (IOS11 && [self.tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [self.tableView setContentInsetAdjustmentBehavior:@(2)];
    }
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
    NSString * xungen = @"中国人，特别是汉族社会中，宗族观念根深蒂固，同姓同宗在汉人眼里是一种很强的关系纽带。而姓氏，则是我们血缘的河流，溯流而上，我们可以追寻到自己的祖根，追访到自己的故乡。中国人姓氏的历史已经有5000余年，是世界上最古老的姓氏在世界其他地区，姓氏的产生不过是近千年的事情。欧洲大陆普遍使用姓的历史只有400年。日本在公元5世纪出现姓多以所居地名为姓，因此多为两字姓，当时日本人一下子涌现出3万多个姓来。中华姓氏起源，可追溯到距今约5000余年的伏羲氏时期。伏羲是中华古史传说时代的远古帝王，在中华民族族谱大系列冠居百王之先，被中华民族誉为人中，位居乏皇五帝之首，文始祖。凝聚. 延续同姓氏在汉人眼里是一种很强的关系纽带，这种微弱的关系以亲情把人与人之间联系起来。通过寻根，把已失散的亲情寻回; 通过寻根，把人与人之间的距离拉近; 通过寻根，把家族史完善继而延续下去。中华民族的传统美德是团结友爱、互相帮助，其形成与文明发展进程有紧密联系的。“团结友爱、互相帮助”的范围往往会有一个模糊概念起着作用。比如:因为中华儿女都是炎黄";
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 10, UIScreenWidth - 30, 30)
                                                     text:xungen
                                                textColor:FirstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    if (self.segementButton.cureentIndex == 0) {
        UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 186, 68)];
        iconImageView.image = [UIImage imageNamed:@"礼根云"];
        iconImageView.st_centerX = UIScreenWidth /2;
        iconImageView.st_top = 20;
        [header addSubview:iconImageView];
        titleLable.top =iconImageView .bottom + 20;
        [header addSubview:titleLable];
        titleLable.text = xungen;
        [NSString lableAutoAdjustheightWithLabel:titleLable];
        header.height = titleLable.bottom;
        self.tableView.tableHeaderView = header;
    }
    if (self.segementButton.cureentIndex == 1) {
        //配置

        NSArray * array = @[@{@"title":@"发表文章",@"imageName":@"发表文章"},
                            @{@"title":@"已发表文章",@"imageName":@"已发表文章"},
                            @{@"title":@"最新发表",@"imageName":@"最新发表"},
                            @{@"title":@"寻根好消息",@"imageName":@"寻根好消息"}
                            ];
        CGFloat buttonWith = UIScreenWidth / 4;
        for (NSInteger i =0 ;i < array.count;i ++) {
            NSDictionary * dic = array[i];
            NSString * imageName = dic[@"imageName"];
            NSString * title = dic[@"title"];
            __weak typeof(self) weakSelf =  self;
            STMenuControl * menuControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonWith) imageName:imageName title:title titleColor:FirstTextColor];
            [menuControl setOnSelctedControl:^(STMenuControl *control) {
                [weakSelf onSelctedMenuControlWithTitle:title];
            }];
            [header addSubview:menuControl];
            
            menuControl.left = i%4 * buttonWith;
            menuControl.top = i/4 * buttonWith;
            header.height = menuControl.bottom;
        }

        self.tableView.tableHeaderView = header;
    }
    if (self.segementButton.cureentIndex == 2) {
        UIView * noheader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0.1)];
        self.tableView.tableHeaderView = noheader;
    }
    
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.segementButton.cureentIndex == 0) {
        return 0;
    }
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [LGFindGenTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10 ;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    LGFindGenTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[LGFindGenTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.navigationController pushViewController:[LGXunGenNewDetailViewController new] animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedMenuControlWithTitle:(NSString*)title{
    if ([title isEqualToString:@"发表文章"]) {
        [self.navigationController pushViewController:[LGFindGenCreateNewViewController new] animated:YES];
    }
}
@end

